﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

namespace FMS.Formulation.Licensing.Engine
{

    internal class WMIComputerSystemInfo
    {
        private const string WIN32_COMPUTERSYSTEM = "Win32_ComputerSystem";

        private const string DOMAINNAME = "Domain";

        private const string MACHINENAME = "Name";

        private const string USERNAME = "UserName";

        private const string WORKGROUPNAME = "Workgroup";

        private string domainName = string.Empty;

        private string machineName = string.Empty;

        private string userName = string.Empty;

        private string workgroupName = string.Empty;

        public string UserName
        {
            get
            {
                return userName;
            }
            internal set
            {
                userName = value;
            }
        }

        public string MachineName
        {
            get
            {
                return machineName;
            }
            internal set
            {
                machineName = value;
            }
        }

        public string DomainName
        {
            get
            {
                return domainName;
            }
            internal set
            {
                domainName = value;
            }
        }

        public string WorkgroupName
        {
            get
            {
                return workgroupName;
            }
            internal set
            {
                workgroupName = value;
            }
        }

        public void Read()
        {
            ManagementClass managementClass = new ManagementClass("Win32_ComputerSystem");
            try
            {
                foreach (ManagementObject instance in managementClass.GetInstances())
                {
                    if (instance.Properties["UserName"].Value != null)
                    {
                        userName = instance.Properties["UserName"].Value.ToString();
                    }
                    if (instance.Properties["Name"].Value != null)
                    {
                        machineName = instance.Properties["Name"].Value.ToString();
                    }
                    if (instance.Properties["Domain"].Value != null)
                    {
                        domainName = instance.Properties["Domain"].Value.ToString();
                    }
                    if (instance.Properties["Workgroup"].Value != null)
                    {
                        workgroupName = instance.Properties["Workgroup"].Value.ToString();
                    }
                }
            }
            catch
            {
            }
        }
    }

}
